﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace YouDaoNotesCodes
{
    /// <summary>
    /// xml增删改查
    /// </summary>
    class XmlOperation
    {
        #region xml添加节点或者特性
        public void Add1()
        {
            #region 加载Xml
            XmlDocument xdoc = new XmlDocument();
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                                        + " <users>"
                                        + " <user id = '1'>"
                                        + " <name>Tom</name>"
                                        + " <password>aaa</password>"
                                        + " </user>"
                                        + " </users>";
            xdoc.LoadXml(xml);
            #endregion

            XmlNode root = xdoc.SelectSingleNode("users");

            #region 添加节点
            //加一个组织标签：单位、部门、职位
            XmlElement Org = xdoc.CreateElement("Org", "nameSpace");
            root.AppendChild(Org);

            XmlElement dw = xdoc.CreateElement("Unit");
            dw.InnerText = "ESG";
            Org.AppendChild(dw);//添加在<Org></Org>标签里！！

            //其他同上
            XmlElement dep = xdoc.CreateElement("Department");
            dep.InnerText = "集团管控";//节点不能加Value赋值
            Org.AppendChild(dep);
            XmlElement pos = xdoc.CreateElement("Position");
            pos.InnerText = "工程师";
            Org.AppendChild(pos);

            #endregion

            #region 添加特性
            //方式1：SetAttribute
            dep.SetAttribute("Desc", "这是职位");

            //方式2：SetAttributeNode
            XmlAttribute OrgAt = xdoc.CreateAttribute("OrgAt");
            OrgAt.Value = "这是组织";//OrgAt.InnerText也可以
            Org.SetAttributeNode(OrgAt);

            #endregion

            //可是如何输出整个xml？@2019-03-05
            Console.WriteLine(xdoc.InnerXml);//这样输出！@2019-07-20
            Console.ReadKey();
        }
        #endregion

        #region xml删除节点或者特性
        public void delete1()
        {
            #region 加载Xml
            XmlDocument xdoc = new XmlDocument();
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                                        + " <users>"
                                        + " <user id = '1'>"
                                        + " <name>Tom</name>"
                                        + " <password>aaa</password>"
                                        + " </user>"
                                        + " </users>";
            xdoc.LoadXml(xml);
            #endregion

            #region 删除节点
            XmlNodeList xl = xdoc.SelectNodes("users/user");
            //删除节点name
            XmlNode xn = xdoc.SelectSingleNode("users/user");
            XmlNode xnChild = xdoc.SelectSingleNode("users/user/name");//name节点
            xn.RemoveChild(xnChild);//删除节点name

            //删除所有子节点
            xn.RemoveAll();

            #endregion

            #region 删除特性
            //删除特性id
            xn.Attributes.RemoveNamedItem("id");

            //删除当前节点的所有特性
            xnChild.Attributes.RemoveAll();

            #endregion

            //输出测试
            Console.WriteLine(xdoc.InnerXml);
            Console.ReadKey();
        }
        #endregion
    }
}
